package com.zhdl.network.asciitcp;

import com.project.command.faq.event.behavior.BehaviorPerform;
import com.project.command.instruction.bo.SingleInstruction;
import com.zhdl.common.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.project.command.instruction.strategy.abs.AbsDic.INSTRUCTION_DEFAULT_HEX;

@Slf4j
@Service
public class AsciiSendService {
    @Resource
    private BehaviorPerform behaviorPerform;

    private void sendCmd(String cmd) {
        SingleInstruction instruction = new SingleInstruction();
        instruction.setInstructionId(IdUtil.getUUID_8());

        //设置要发送的指令字符串
        //转换成十进制
        instruction.setInstruction(cmd);

        //INSTRUCTION_DEFAULT_HEX 原始字符串字符串
        //INSTRUCTION_DEFAULT 进行转码后发送执行
        instruction.setBeforeInstructionName(INSTRUCTION_DEFAULT_HEX);
        //默认使用com.zhdl.instruction.strategy包下后置策略
        instruction.setAfterInstructionName(AsciiAfterService.ASCII_TCP_AFTER_SERVICE);
        //客户端通信地址, 串口为数字, 网口为ip+端口       数据来源 : port_comm 或 port_socket 表
//        instruction.setPortKey("3");
        //东区的电机串口待定 -- 可能是3 测试的时候为了避免冲突 设为 1
        instruction.setPortKey("");
        //设置协议  数据来源 : protocols 表
        instruction.setProtocols(AsciiTcpAnalysisImpl.ASCII_TCP_ANALYSIS);
        behaviorPerform.perform(instruction);
    }

    public void sendLocation(String cmd) {
        sendCmd(cmd);
    }
}
